//
//  CLAdsorbButton.m
//  Charles
//
//  Created by Leo on 2017/3/17.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CLAdsorbButton.h"


//枚举四个吸附方向
typedef enum {
    CLAdsorbDirectionLEFT,
    CLAdsorbDirectionRIGHT,
    CLAdsorbDirectionTOP,
    CLAdsorbDirectionBOTTOM
}CLAdsorbDirection;


@interface CLAdsorbButton()

@property(nonatomic,assign) CGPoint startPos;   //起始位置

@end

@implementation CLAdsorbButton

/**
 *  开始触摸，记录触点位置用于判断是拖动还是点击
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    // 获得触摸在根视图中的坐标
    UITouch *touch = [touches anyObject];
    _startPos = [touch locationInView:_rootView];
}

/**
 *  手指按住移动过程,通过悬浮按钮的拖动事件来拖动整个悬浮窗口
 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // 获得触摸在根视图中的坐标
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:_rootView];
    // 移动按钮到当前触摸位置
    self.superview.center = curPoint;
}

/**
 *  拖动结束后使悬浮窗口吸附在最近的屏幕边缘
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (!self.isAdsorb) {
        // 获得触摸在根视图中的坐标
        UITouch *touch = [touches anyObject];
        CGPoint curPoint = [touch locationInView:_rootView];
        // 通知代理,如果结束触点和起始触点极近则认为是点击事件
        if (pow((_startPos.x - curPoint.x),2) + pow((_startPos.y - curPoint.y),2) < 1) {
            if ([self.delegate respondsToSelector:@selector(adsorbButtonClicked:)]) {
                [self.delegate adsorbButtonClicked:self];
            }
            // 点击后不吸附
            return;
        }

        [UIView animateWithDuration:0.3 animations:^{
            self.superview.frame = self.originFrame;
        }];
        return;
    }else {
        // 获得触摸在根视图中的坐标
        UITouch *touch = [touches anyObject];
        CGPoint curPoint = [touch locationInView:_rootView];
        // 通知代理,如果结束触点和起始触点极近则认为是点击事件
        if (pow((_startPos.x - curPoint.x),2) + pow((_startPos.y - curPoint.y),2) < 1) {
            if ([self.delegate respondsToSelector:@selector(adsorbButtonClicked:)]) {
                [self.delegate adsorbButtonClicked:self];
            }
            // 点击后不吸附
            return;
        }
        CGFloat left = curPoint.x;
        CGFloat right = SCREEN_WIDTH - curPoint.x;
        CGFloat top = curPoint.y;
        CGFloat bottom = SCREEN_HEIGHT - curPoint.y;
        // 计算四个距离最小的吸附方向
        CLAdsorbDirection minDirection = CLAdsorbDirectionLEFT;
        CGFloat minDistance = left;
        if (right < minDistance) {
            minDistance = right;
            minDirection = CLAdsorbDirectionRIGHT;
        }
        if (top < minDistance) {
            minDistance = top;
            minDirection = CLAdsorbDirectionTOP;
        }
        if (bottom < minDistance) {
            minDirection = CLAdsorbDirectionBOTTOM;
        }
        // 开始吸附
        switch (minDirection) {
            case CLAdsorbDirectionLEFT:{
                [UIView animateWithDuration:0.3 animations:^{
                    self.superview.center = CGPointMake(self.superview.frame.size.width/2, self.superview.center.y);
                }];
                break;
            }
            case CLAdsorbDirectionRIGHT:{
                [UIView animateWithDuration:0.3 animations:^{
                    self.superview.center = CGPointMake(SCREEN_WIDTH - self.superview.frame.size.width/2, self.superview.center.y);
                }];
                break;
            }
            case CLAdsorbDirectionTOP:{
                [UIView animateWithDuration:0.3 animations:^{
                    self.superview.center = CGPointMake(self.superview.center.x, self.superview.frame.size.height/2);
                }];
                break;
            }
            case CLAdsorbDirectionBOTTOM:{
                [UIView animateWithDuration:0.3 animations:^{
                    self.superview.center = CGPointMake(self.superview.center.x, SCREEN_HEIGHT - self.superview.frame.size.height/2);
                }];
                break;
            }
            default:
                break;
        }
    }
}

@end
